package com.baizhi.service.impl;

import com.baizhi.dao.TagDAO;
import com.baizhi.entity.Tag;
import com.baizhi.service.tagService;
import com.baizhi.util.MybatisUtil;

import java.util.List;

public class TagServiceImpl implements tagService {

    @Override  //查询所有
    public List<Tag> queryAll() {
        List<Tag> list = null;
        try {

            TagDAO td = (TagDAO) MybatisUtil.getMapper(TagDAO.class);
            list = td.selectAll();
            MybatisUtil.commit();

        }catch (Exception e){
            e.printStackTrace();
            MybatisUtil.rollback();  //向上回滚
            throw new RuntimeException(e);
        }finally {
            MybatisUtil.close();  //关闭资源
        }
        return list;
    }


    @Override  //根据id 删除一条数据
    public void deleteById(int id) {

        try {

            TagDAO td = (TagDAO) MybatisUtil.getMapper(TagDAO.class);
            td.deleteById(id);
            MybatisUtil.commit();
        }catch (Exception e){
            e.printStackTrace();
            MybatisUtil.rollback();  //向上回滚
            throw new RuntimeException(e);

        }finally {
            MybatisUtil.close();  //关闭资源
        }
    }


    @Override  //插入一条数据
    public void insertTag(Tag tag) {

        try{

            TagDAO td = (TagDAO) MybatisUtil.getMapper(TagDAO.class);
            td.insertTag(tag);
            MybatisUtil.commit();
        }catch (Exception e){
            e.printStackTrace();
            MybatisUtil.rollback();  //向上回滚
            throw new RuntimeException(e);
        }finally {
            MybatisUtil.close();  //关闭资源
        }
    }

    @Override
    public List<Tag> queryAllTagByType(String type) {
        TagDAO td = (TagDAO) MybatisUtil.getMapper(TagDAO.class);
        List<Tag> tags = td.selectAllByType(type);
        MybatisUtil.close();
        return tags;
    }
}
